package com.china.produce.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2023/7/2
 */
public class DemoExecutorRegistryThread {

    private Logger logger = LoggerFactory.getLogger(DemoExecutorRegistryThread.class);

    private volatile boolean toStop = false;

    public Thread registerThread;

    private static DemoExecutorRegistryThread  instance = new DemoExecutorRegistryThread();

    public static DemoExecutorRegistryThread getInstance() {

        return instance;
    }

    public void stop() {
        toStop = true;
        registerThread.interrupt();
        try {
            registerThread.join();
        } catch (InterruptedException e) {
            logger.error(e.getMessage(), e);
        }
    }

    public void start(String appName, String address) {
         registerThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while(!toStop) {
                    logger.info("thread name {} state {} register success", Thread.currentThread().getName(), Thread.currentThread().getState());
                    if (!toStop) {
                        try {
                            TimeUnit.SECONDS.sleep(2);
                        }catch (Exception e) {

                        }
                    }
                }
                try{
                    logger.info("thread name {} state {} remove success", Thread.currentThread().getName(), Thread.currentThread().getState());
                }catch (Exception e) {

                }
            }
        });

        registerThread.setDaemon(true);
        registerThread.setName("demo execute");
        registerThread.start();
    }
}
